﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cMenuCategory : System.Web.UI.UserControl
    {
        NewsManagement newsMana = new NewsManagement();
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.Language language;
        string modulCode;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];
            loadModul();

            if (Request.Cookies[CONST.COOKIES_LANGUAGE].Value == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }
            if (!Page.IsPostBack)
            {
                this.LoadTreeView();
            }
        }

        protected void loadLanguage(string modulCode)
        {
            lbNameModul.Text = languageMana.GetTagValueByLanguageIDandTagName(modulCode, language.LanguagesID);
        }

        protected void loadModul()
        {
            switch (modulCode)
            {
                case "MNEWS":
                    {
                        
                    }
                    break;
                case "MSHAREHOLDERS":
                    {
                        
                    }
                    break;
                case "MJOB":
                    {
                        
                    }
                    break;
                case "MHELP":
                    {
                        
                    }
                    break;
                case "MCONTACT":
                    {
                        
                    }
                    break;
                case "MINTRO":
                    {
                        
                    }
                    break;
                case "MPRODUCT":
                    { 
                    
                    }
                    break;
            }


        }

        protected void LoadTreeView()
        {
            try
            {
                modulCode = Request.QueryString["Mod"];
                if (modulCode == "MPRODUCT")
                {
                    loadLanguage(modulCode);
                    List<TypeProduct> listParent = proMana.GetParentTypeProductAll(language.LanguagesID);
                    foreach (var pTypeProduct in listParent)
                    {
                        TreeNode root = new TreeNode();
                        root.Text = "<span>"+pTypeProduct.TypeProductName.ToString()+"</span>";
                        //root.NavigateUrl = Request.CurrentExecutionFilePath + "?"  + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + pTypeProduct.TypeProductID.ToString() + "&Mod=" + modulCode;
                        root.NavigateUrl = "~/Hem/san-pham-dich-vu/" + pTypeProduct.TypeProductID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(proMana.GetNametypeByID(pTypeProduct.TypeProductID).Replace(" ", "-")) + ".html";
                        root.ToolTip = pTypeProduct.TypeProductName.ToString();
                        List<TypeProduct> listChild = proMana.GetChildrenTypeProductByParentIDAll((int)pTypeProduct.TypeProductID, (int)language.LanguagesID);
                        foreach (var cTypeProduct in listChild)
                        {
                            TreeNode child = new TreeNode();
                            child.Text = "<span>"+cTypeProduct.TypeProductName.ToString()+"</span>";
                            //child.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + cTypeProduct.TypeProductID.ToString() + "&Mod=" + modulCode;
                            child.NavigateUrl = "~/Hem/san-pham-dich-vu/" + cTypeProduct.TypeProductID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(proMana.GetNametypeByID(cTypeProduct.TypeProductID).Replace(" ", "-")) + ".html";
                            child.ToolTip = cTypeProduct.TypeProductName.ToString();
                            root.ChildNodes.Add(child);
                        }
                        TreeViewCagtegory.Nodes.Add(root);
                    }

                    TreeViewCagtegory.CollapseAll();
                }
                else 
                {
                    loadLanguage(modulCode);
                    List<Category> listParent = newsMana.GetParentCategoryAll(language.LanguagesID, modulCode);
                    foreach (var pNews in listParent)
                    {
                        TreeNode root = new TreeNode();
                        root.Text ="<span>"+ pNews.CategoryName.ToString()+"</span>";
                       // root.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_CATEGORY + "=" + pNews.CategoryID.ToString() + "&Mod=" + modulCode;
                        switch(modulCode)
                        {
                            case "MNEWS":
                                {
                                    root.NavigateUrl = "~/Hem/tin-tuc/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                            case "MSHAREHOLDERS":
                                {
                                    root.NavigateUrl = "~/Hem/quan-he-co-dong/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                            case "MJOB":
                                {
                                    root.NavigateUrl = "~/Hem/tuyen-dung/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                            case "MHELP":
                                {
                                    root.NavigateUrl = "~/Hem/tro-giup/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                            case "MCONTACT":
                                {
                                    root.NavigateUrl = "~/Hem/lien-he/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                            case "MINTRO":
                                {
                                    root.NavigateUrl = "~/Hem/gioi-thieu/" + pNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(pNews.CategoryName.Replace(" ", "-")) + ".html";
                                }
                                break;
                        }
                            
                        root.ToolTip ="<span>"+ pNews.CategoryName.ToString()+"</span>";
                        List<Category> listChildNews = newsMana.GetChildrenCategoryByParentIDAll((int)pNews.CategoryID, (int)language.LanguagesID, modulCode);
                        foreach (var cNews in listChildNews)
                        {
                            TreeNode child = new TreeNode();
                            child.Text ="<span>"+ cNews.CategoryName.ToString()+"</span>";
                            //child.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_CATEGORY + "=" + cNews.CategoryID.ToString() + "&Mod=" + modulCode;
                            switch (modulCode)
                            {
                                case "MNEWS":
                                    {
                                        child.NavigateUrl = "~/Hem/tin-tuc/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                                case "MSHAREHOLDERS":
                                    {
                                        child.NavigateUrl = "~/Hem/quan-he-co-dong/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                                case "MJOB":
                                    {
                                        child.NavigateUrl = "~/Hem/tuyen-dung/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                                case "MHELP":
                                    {
                                        child.NavigateUrl = "~/Hem/tro-giup/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                                case "MCONTACT":
                                    {
                                        child.NavigateUrl = "~/Hem/lien-he/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                                case "MINTRO":
                                    {

                                        child.NavigateUrl = "~/Hem/gioi-thieu/" + cNews.CategoryID.ToString() + "/" + modulCode + "/" + overServices.ConvertString(cNews.CategoryName.Replace(" ", "-")) + ".html";
                                    }
                                    break;
                            }
                            
                            child.ToolTip = cNews.CategoryName.ToString();

                            root.ChildNodes.Add(child);
                        }
                        TreeViewCagtegory.Nodes.Add(root);
                    }
                }
                
                TreeViewCagtegory.CollapseAll();
            }
            catch (Exception)
            {
               
            }
        }

        protected void TreeViewCagtegory_SelectedNodeChanged(object sender, EventArgs e)
        {

        }
    }
}